package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.mapper.AnnouncementMapper;
import com.bayihouse.model.entity.Announcement;
import com.bayihouse.model.entity.FileInfo;
import com.bayihouse.model.request.announcement.AnnouncementQueryRequest;
import com.bayihouse.model.vo.AnnouncementDetailVO;
import com.bayihouse.model.vo.AnnouncementVO;
import com.bayihouse.model.vo.FileVO;
import com.bayihouse.service.AdministerService;
import com.bayihouse.service.AnnouncementService;
import com.bayihouse.service.FileInfoService;
import com.bayihouse.utils.MinioUtils;
import com.bayihouse.utils.SqlUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import jnr.ffi.annotations.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【announcement】的数据库操作Service实现
 * @createDate 2025-04-26 19:47:17
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
        implements AnnouncementService {


    @Resource
    private MinioUtils minioService;

    @Resource
    private AdministerService administerService;

    @Resource
    private FileInfoService fileInfoService;

    @Override
    public List<AnnouncementVO> getAnnouncementVOList(List<Announcement> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getAnnouncementVO).collect(Collectors.toList());
    }

    @Override
    public AnnouncementVO getAnnouncementVO(Announcement anncouncement) {
        if (anncouncement == null) {
            return null;
        }
        Gson gson = new Gson();
        AnnouncementVO announcementVO = AnnouncementVO.objToVo(anncouncement);
        try {
            FileInfo byId = fileInfoService.getById(anncouncement.getHeadimageid());
            announcementVO.setHeadimageUrl(minioService.getUrl(byId.getFileName()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String userName = administerService.getById(anncouncement.getAdministerid()).getUserName();
        announcementVO.setAdministerName(userName);
        return announcementVO;
    }

    @Override
    public AnnouncementDetailVO getAnnouncementDetailVO(Announcement anncouncement) throws Exception {
        if (anncouncement == null) {
            return null;
        }
        Gson gson = new Gson();
        AnnouncementDetailVO announcementDetailVO = AnnouncementDetailVO.objToVo(anncouncement);
        String fileidsStr = anncouncement.getFileids();
        if (fileidsStr != null && !fileidsStr.equals("")) {
            List<Long> fileids = gson.fromJson(fileidsStr, new TypeToken<List<Long>>() {
            }.getType());
            if (fileids.size() != 0) {
                List<FileInfo> fileInfos = fileInfoService.listByIds(fileids);
                List<FileVO> fileVOS = new ArrayList<>();
                for (int i = 0; i < fileids.size(); i++) {
                    FileVO fileVO = new FileVO();
                    fileVO.setId(fileInfos.get(i).getId());
                    fileVO.setFileUrl(minioService.getUrl(fileInfos.get(i).getFileName()));
                    Integer newFileNameIndex = fileInfos.get(i).getFileName().lastIndexOf("//");
                    fileVO.setFileName(fileInfos.get(i).getFileName().substring(newFileNameIndex + 2));
                    fileVOS.add(fileVO);
                }
                announcementDetailVO.setOtherFiles(fileVOS);
            }
        }
        if (anncouncement.getHeadimageid() != null) {
            FileInfo byId = fileInfoService.getById(anncouncement.getHeadimageid());
            announcementDetailVO.setHeadimageUrl(minioService.getUrl(byId.getFileName()));
        }
        String userName = administerService.getById(anncouncement.getAdministerid()).getUserName();
        announcementDetailVO.setAdministerName(userName);
        return announcementDetailVO;
    }


    @Override
    public Wrapper<Announcement> getQueryWrapper(AnnouncementQueryRequest anncouncementQueryRequest) {
        String content = anncouncementQueryRequest.getContent();
        String telephone = anncouncementQueryRequest.getTelephone();
        String authername = anncouncementQueryRequest.getAuthername();
        String title = anncouncementQueryRequest.getTitle();
        long current = anncouncementQueryRequest.getCurrent();
        long pageSize = anncouncementQueryRequest.getPageSize();
        String sortField = anncouncementQueryRequest.getSortField();
        String sortOrder = anncouncementQueryRequest.getSortOrder();
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(StringUtils.isNotBlank(telephone), "telephone", telephone);
        queryWrapper.eq(StringUtils.isNotBlank(authername), "authername", authername);
        queryWrapper.eq(StringUtils.isNotBlank(title), "title", title);
        sortField = "createTime";
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_DESC),
                sortField);
        return queryWrapper;
    }
}




